/* *****************************************************************************

 Copyright (C) : 2000-2024, unit

 ******************************************************************************
 File Name     : User_App.c
 Version       : ver 1.0
 Author        : phh
 Created       : 2024/10/31
 Last Modified :
 Description   :
 Function List :
 History       :
 1.Date        : 2024/10/31
   Author      : phh
   Modification: Created file

***************************************************************************** */

/* ----------------------------------------------*
 * header files                                  *
 *---------------------------------------------- */
#include "User_App.h"

#include "osal/string.h"
#include "osal/task.h"
#include "utlist.h"
#include "stream_frame.h"
#include "osal/semaphore.h"
#include "osal/mutex.h"
#include "custom_mem/custom_mem.h"

#include "sys_config.h"
#include "typesdef.h"
#include "lib/video/dvp/cmos_sensor/csi.h"
#include "dev.h"
#include "devid.h"
#include "hal/gpio.h"
#include "hal/lcdc.h"
#include "hal/spi.h"
#include "osal/irq.h"
#include "osal/string.h"
#include "dev/vpp/hgvpp.h"
#include "dev/scale/hgscale.h"
#include "dev/jpg/hgjpg.h"
#include "dev/lcdc/hglcdc.h"
#include "osal/semaphore.h"
#include "lib/lcd/lcd.h"
#include "lib/lcd/gui.h"
#include "dev/vpp/hgvpp.h"
#include "dev/csi/hgdvp.h"
#include "lib/video/dvp/jpeg/jpg.h"



#include "Bsp_Data.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

/* ----------------------------------------------*
 * macro                                         *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * type definition                               *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * module-wide global variables                  *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * modul global variables                        *
 *---------------------------------------------- */

static k_task_handle_t user_app_hd;
/* ----------------------------------------------*
 * internal routine prototypes                   *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * routines' implementations                     *
 *---------------------------------------------- */




/* ****************************************************************************
 Prototype     : Key_Scan
 Description   :
 Input         : None
 Output        : None
 Return Value  :
 Calls         :
 Called By     :

 History
 1.Date        : 2024/10/31
   Author      : phh
   Modification: Created function

**************************************************************************** */
uint8_t Key_Scan( void )
{

    static uint8_t  keyStatus=0;
    uint8_t keyValue=0;
    switch ( keyStatus )
    {
    case 0:
        if ( !gpio_get_val(PA_1) )
        {
            keyStatus=1;
        }
        break;
    case 1:
        //仍然按下,消抖
        if ( !gpio_get_val(PA_1) )
        {
            keyValue=1;
            keyStatus=2;
        }
        else
        {
            keyValue=0;
        }
        break;
    case 2:
        //等待释放
        if ( gpio_get_val(PA_1) )
        {
            keyStatus=0;
        }
        break;
    default:
        break;
    }


    return keyValue;

}



/* ****************************************************************************
 Prototype     : Key_Scan
 Description   :
 Input         : None
 Output        : None
 Return Value  :
 Calls         :
 Called By     :

 History
 1.Date        : 2024/10/31
   Author      : phh
   Modification: Created function

**************************************************************************** */
uint8_t Key2_Scan( void )
{

    static uint8_t  keyStatus=0;
    uint8_t keyValue=0;
    switch ( keyStatus )
    {
    case 0:
        if ( !gpio_get_val(PA_3) )
        {
            keyStatus=1;
        }
        break;
    case 1:
        //仍然按下,消抖
        if ( !gpio_get_val(PA_3) )
        {
            keyValue=1;
            keyStatus=2;
        }
        else
        {
            keyValue=0;
        }
        break;
    case 2:
        //等待释放
        if ( gpio_get_val(PA_3) )
        {
            keyStatus=0;
        }
        break;
    default:
        break;
    }


    return keyValue;

}

/* ****************************************************************************
 Prototype     : User_App_Handle
 Description   :
 Input         : None
 Output        : None
 Return Value  :
 Calls         :
 Called By     :

 History
 1.Date        : 2024/10/31
   Author      : phh
   Modification: Created function

**************************************************************************** */
void User_App_Handle( void )
{
    uint8_t keyPress=0;
    uint8_t key2Press=0;
    while ( 1 )
    {
        keyPress=Key_Scan(  );
        if ( keyPress )
        {
            //启动拍照
            stPhoto.key=1;
            os_printf("photo pressed \r\n");
        }
        key2Press=Key2_Scan(  );
        if ( key2Press )
        {
            struct lcdc_device *lcd_dev;
            lcd_dev = (struct lcdc_device *)dev_get(HG_LCDC_DEVID);
            //启动录像
            lcdc_set_video_en(lcd_dev,1);
            os_printf("video pressed \r\n");
        }

        os_sleep_ms( 10);

    }




}


/* ****************************************************************************
 Prototype     : Thread_Creat
 Description   :
 Input         : None
 Output        : None
 Return Value  :
 Calls         :
 Called By     :

 History
 1.Date        : 2024/10/31
   Author      : phh
   Modification: Created function

**************************************************************************** */
void Thread_Creat( void )
{

    csi_kernel_task_new((k_task_entry_t)User_App_Handle, "User_App", NULL, KPRIO_NORMAL, 0, NULL, 4096, &user_app_hd);

}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

